package com.jiayang.hhotel.ui;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.jiayang.hhotel.R;
import com.jiayang.hhotel.bean.Constant;
import com.jiayang.hhotel.bean.NowLiveDetailsModel;
import com.jiayang.hhotel.bean.NowLiveSearchModel;
import com.jiayang.hhotel.bean.RoomNumModel;
import com.jiayang.hhotel.presenter.NowLiveSearchDetailsPresenter;
import com.jiayang.hhotel.util.CommonUtil;
import com.jiayang.hhotel.util.DateUtil;
import com.jiayang.hhotel.util.FlashUtil;
import com.jiayang.hhotel.util.ScreenUtil;
import com.jiayang.hhotel.util.StringUtil;
import com.jiayang.hhotel.util.ToastUtil;

import java.util.Calendar;
import java.util.List;

import cn.droidlover.xdroidmvp.mvp.XActivity;

/**
 * Created by Administrator on 2017/5/24 0024.
 * 现住查询详情页面
 */

public class NowLiveSearchDetailsActivity extends XActivity<NowLiveSearchDetailsPresenter> implements View.OnClickListener, DateUtil.PickerDeterListener {

    private View layout;
    private ImageView icon;
    private TextView tv;

    private TextView roomNum;//房号显示
    private ImageView more;//更多

    private ScrollView sc;

    private LinearLayout addInfo,addPersonalInfo,addAdditionalInfo,menuParent;//添加信息,个人信息，附加信息，菜单容器

    private Button next,//底部按钮
                    edit,//编辑菜单
                    modifyDate,//改期
                    changeRoom,//换房
                    lineRoom,//连房
                    prePay;//预付

    private Calendar c;

    private DatePicker picker;

    private AlertDialog.Builder builder;

    private NowLiveSearchModel.PreceptionListDtoBean model;
    private NowLiveDetailsModel nowLiveDetailsModel;//现住详情数据类

    private NowLiveDetailsModel.Preception1ListDtoBean preception1ListDtoBean;
    private List<NowLiveDetailsModel.Preception2ListDtoBean> preception2ListDtoBeanList;

    private String[] RoomTitleList;//用于显示房号标题
    private String[] PersonTitleList;//用于显示个人信息标题
    private String[] AddTitleList;//用于显示附加信息标题
    private String[] RoomContent;//房号板块内容
    private String[] PersonContent;//个人板块内容
    private String[] AddInfoContent;//附加信息板块内容

    @Override
    public void initData(Bundle savedInstanceState) {
        initTitle(this,true,getString(R.string.now_live_details),R.color.orange_color,null,null);

        model = (NowLiveSearchModel.PreceptionListDtoBean) getIntent().getExtras().getSerializable(Constant.INTENT_PROJECT_KEY);

        layout = findViewById(R.id.ll_data);
        icon = (ImageView) findViewById(R.id.iv_pic);
        tv = (TextView) findViewById(R.id.tv_text);

        roomNum= (TextView) findViewById(R.id.tv_room_num);
        more= (ImageView) findViewById(R.id.iv_more);
        addInfo= (LinearLayout) findViewById(R.id.add_info);
        addPersonalInfo= (LinearLayout) findViewById(R.id.add_personal_info);
        addAdditionalInfo= (LinearLayout) findViewById(R.id.add_additional_info);
        sc = (ScrollView) findViewById(R.id.scrollView);
        next= (Button) findViewById(R.id.bt_next);

        edit= (Button) findViewById(R.id.bt_edit);
        modifyDate= (Button) findViewById(R.id.tv_modify_date);
        changeRoom= (Button) findViewById(R.id.tv_change_room);
        lineRoom= (Button) findViewById(R.id.tv_line_room);
        prePay= (Button) findViewById(R.id.tv_pay);
        menuParent= (LinearLayout) findViewById(R.id.menu_parent);

        more.setOnClickListener(this);
        next.setOnClickListener(this);

        RoomTitleList = getResources().getStringArray(R.array.now_live_details_room_num_info_list);
        PersonTitleList = getResources().getStringArray(R.array.now_live_details_person_info_list);
        AddTitleList = getResources().getStringArray(R.array.now_live_details_add_info_list);

        sc.setVisibility(View.GONE);
        CommonUtil.showProgressDialog(this,R.string.loading);
        getP().getDetails(Constant.LAUGUAGE_FLAG_CHINESE,model.getRegister());
    }

    @Override
    public int getLayoutId() {
        return R.layout.layout_now_live_search_details;
    }

    @Override
    public NowLiveSearchDetailsPresenter newP() {
        return new NowLiveSearchDetailsPresenter();
    }

    /**
     * 添加信息
     */
    private void addInfo(ViewGroup view,String title,String content){
        TextView itemView = new TextView(this);
        itemView.setTextColor(Color.parseColor("#f0f0f0"));
        itemView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
        itemView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        itemView.setPadding(ScreenUtil.dip2px(this,20),ScreenUtil.dip2px(this,15),0,0);
        itemView.setText(title + getString(R.string.space) + content);
        view.addView(itemView);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_more://更多
                if(flag != -1){
                    menuAnim();
                }
                break;
            case R.id.bt_next://下一个人信息
                if(menuParent.getVisibility()==View.GONE){
                    index++;
                    initOtherInfo();
                }else{
                    menuAnim();
                }
                break;
            case R.id.bt_edit://编辑
                if(menuParent.getVisibility()==View.VISIBLE){
                    menuAnim();
                }
                break;
            case R.id.tv_modify_date://改期
                if(menuParent.getVisibility()==View.VISIBLE){
                    menuAnim();
                    DateUtil.initDatePicker(this,this);
                }
                break;
            case R.id.tv_change_room://换房
                if(menuParent.getVisibility()==View.VISIBLE){
                    menuAnim();
                    getP().initRoomList(Constant.LAUGUAGE_FLAG_CHINESE,preception1ListDtoBean.getArrDate(),preception1ListDtoBean.getDptDate(),1,preception1ListDtoBean.getRmType());
                }
                break;
            case R.id.tv_line_room://连房
                if(menuParent.getVisibility()==View.VISIBLE){
                    menuAnim();
                    getP().createLineRoomDialog(this);
                }
                break;
            case R.id.tv_pay://预付
                if(menuParent.getVisibility()==View.VISIBLE){
                    menuAnim();
                    Intent pay = new Intent(this,PrePayActivity.class);
                    Bundle bundle=new Bundle();
                    bundle.putSerializable(Constant.INTENT_PROJECT_KEY,nowLiveDetailsModel);
                    bundle.putInt(Constant.INTENT_INT_KEY,index);
                    pay.putExtras(bundle);
                    startActivity(pay);
                }
                break;
            case R.id.menu_parent://设置点击菜单外边缘也可以隐藏菜单
                if(menuParent.getVisibility()==View.VISIBLE){
                    menuAnim();
                }
                break;
        }
    }

    @Override
    public void onBackPressed() {
        if(menuParent.getVisibility()==View.VISIBLE){
            menuAnim();
        }else{
            super.onBackPressed();
        }

    }

    private void menuAnim(){
        if(menuParent.getVisibility()==View.VISIBLE){
            edit.setOnClickListener(null);
            modifyDate.setOnClickListener(null);
            changeRoom.setOnClickListener(null);
            lineRoom.setOnClickListener(null);
            prePay.setOnClickListener(null);
            menuParent.cancelPendingInputEvents();
//            menuParent.setOnClickListener(null);
            FlashUtil.TranslateUPAnim(menuParent);
            menuParent.setVisibility(View.GONE);
        }else{
            edit.setOnClickListener(this);
            modifyDate.setOnClickListener(this);
            changeRoom.setOnClickListener(this);
            lineRoom.setOnClickListener(this);
            prePay.setOnClickListener(this);
//            menuParent.setOnClickListener(this);
            FlashUtil.TranslateDownAnim(menuParent);
            menuParent.setVisibility(View.VISIBLE);
        }
    }

    //获取到现住详情数据
    public void setNowLiveDetailsModel(NowLiveDetailsModel model){
        this.nowLiveDetailsModel = model;
        this.preception1ListDtoBean = model.getPreception1ListDto().get(0);
        this.preception2ListDtoBeanList = model.getPreception2ListDto();
        roomNum.setText(getString(R.string.room_num)+getString(R.string.space)+preception1ListDtoBean.getRoom());//房号
        RoomContent = new String[]{preception1ListDtoBean.getArrDate(), preception1ListDtoBean.getDptDate()
        , preception1ListDtoBean.getRmType(), preception1ListDtoBean.getFolioRate()+"", preception1ListDtoBean.getRmRate()+""
        , preception1ListDtoBean.getPackages(), preception1ListDtoBean.getGstType(), preception1ListDtoBean.getOrgCode()
        , preception1ListDtoBean.getMarketdescp()};
        for (int i = 0; i< RoomTitleList.length; i++){
            addInfo(addInfo,RoomTitleList[i],RoomContent[i]);
        }
        initOtherInfo();
        CommonUtil.dismissProgressDialog(this);
        sc.setVisibility(View.VISIBLE);
        flag = 0;
    }

    private int index = 0;//显示个人信息的下标值
    /**
     * 设置点击下一个人的信息的数据以及显示
     */
    private void initOtherInfo(){
        if(preception2ListDtoBeanList.size()-1 >= index){//没有到最后一个人
            NowLiveDetailsModel.Preception2ListDtoBean model = preception2ListDtoBeanList.get(index);
            PersonContent = new String[]{model.getChnName(),model.getEngFirstName(),model.getPhone(),model.getSex()==0?getString(R.string.man):getString(R.string.woman)};
            AddInfoContent = new String[]{model.getBirthDate(),model.getHeadshipDescp(),model.getAppellationDescp(),model.getFancy(),model.getTaboo(),model.getDieteticHabit(),model.getRoomService()};
            addPersonalInfo.removeAllViews();
            addAdditionalInfo.removeAllViews();
            for(int i = 0;i<PersonTitleList.length;i++){
                addInfo(addPersonalInfo,PersonTitleList[i],PersonContent[i]);
            }
            for(int i = 0;i < AddInfoContent.length;i++){
                addInfo(addAdditionalInfo,AddTitleList[i],AddInfoContent[i]);
            }
        }else{
            ToastUtil.Toast(getString(R.string.already_arrive_last_person),this);
        }
    }

//    /**
//     * 初始化日期选择
//     */
//    private void initDatePicker(){
//        c = Calendar.getInstance();
//        View pickerSelect= LayoutInflater.from(this).inflate(R.layout.can_rent_room_picker_select,null);
//        picker = (DatePicker) pickerSelect.findViewById(R.id.dp_year);
//        picker.init(c.get(Calendar.YEAR),c.get(Calendar.MONTH),c.get(Calendar.DAY_OF_MONTH),null);
//        builder = new AlertDialog.Builder(this);
//        builder.setView(pickerSelect);
//        builder.setNegativeButton(getString(R.string.determine), new DialogInterface.OnClickListener() {
//            @Override
//            public void onClick(DialogInterface dialog, int which) {
//                int cuYear,cuMonth,cuDay;
//                cuYear=picker.getYear();
//                cuMonth=picker.getMonth() + 1;
//                cuDay=picker.getDayOfMonth();
//                String str=cuYear+"-"+ StringUtil.AddZero(cuMonth) +"-"+StringUtil.AddZero(cuDay);
//                if(DateUtil.TimeIsLess(str,"yyyy-MM-dd")||DateUtil.TimeIsEtc(str,"yyyy-MM-dd")){//选择的日期是未来时间，包括当天
//                    getP().correctionDate(Constant.LAUGUAGE_FLAG_CHINESE,preception1ListDtoBean.getRegister(),str);
//                }else{
//                    ToastUtil.Toast(NowLiveSearchDetailsActivity.this.getString(R.string.select_time_timeout),NowLiveSearchDetailsActivity.this);
//                }
//            }
//        });
//        builder.create();
//        builder.show();
//    }
    public void initDivideDialog(List<RoomNumModel.PRsvforcastRoomDtoListBean> list){
        getP().createModifyRoomDialog(this,list);
    }

    public void clickDeter(String room){
        getP().huanFangSave(Constant.LAUGUAGE_FLAG_CHINESE,preception1ListDtoBean.getRegister(),room);
    }

    public void clickLineRoomDeter(int mpType){
        getP().LineRoomSave(Constant.LAUGUAGE_FLAG_CHINESE,0,preception1ListDtoBean.getRegister(),preception1ListDtoBean.getRoom(),mpType);
    }

    private int flag = -1;//请求返回码（判断是否返回了正常数据）(-1：返回错误;0:正常返回)
    /**
     * 请求错误
     * @param state 状态
     */
    public void sealPic(final int state){
        flag = -1;
        CommonUtil.dismissProgressDialog(this);
        layout.setVisibility(View.VISIBLE);
        switch (state){
            case 1002://没有数据
                break;
            case -1://服务器错误
                icon.setImageResource(R.mipmap.ic_launcher);
                tv.setText(getString(R.string.service_error));
                icon.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        layout.setVisibility(View.GONE);
                        CommonUtil.showProgressDialog(NowLiveSearchDetailsActivity.this,R.string.loading);
                        getP().getDetails(Constant.LAUGUAGE_FLAG_CHINESE,model.getRegister());
                    }
                });
                break;
    default://其他错误
            icon.setImageResource(R.mipmap.ic_launcher);
                tv.setText(getString(R.string.net_error));
                icon.setOnClickListener(null);
                break;
}
    }

    @Override
    public void onPicker(String date) {
        if(DateUtil.TimeIsLess(date,"yyyy-MM-dd")||DateUtil.TimeIsEtc(date,"yyyy-MM-dd")){//选择的日期是未来时间，包括当天
            getP().correctionDate(Constant.LAUGUAGE_FLAG_CHINESE,preception1ListDtoBean.getRegister(),date);
        }else{
            ToastUtil.Toast(NowLiveSearchDetailsActivity.this.getString(R.string.select_time_timeout),NowLiveSearchDetailsActivity.this);
        }
    }

//    @Override
//    public boolean onTouch(View view, MotionEvent motionEvent) {
//        if(menuParent.getVisibility()==View.VISIBLE){
//            sc.requestDisallowInterceptTouchEvent(false);
//        }else{
//            sc.requestDisallowInterceptTouchEvent(true);
//        }
//        return false;
//    }
}
